<?php

namespace app\admin\controller\user;

use app\api\controller\Pktime;
use app\api\controller\Preparetime;
use app\api\controller\Usertime;
use app\common\controller\Backend;

/**
 * 会员管理
 *
 * @icon fa fa-user
 */
class User extends Backend
{

    protected $relationSearch = true;


    /**
     * @var \app\admin\model\User
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('User');
    }

    /**
     * 查看
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                ->with('group')
                ->where($where)
                ->order($sort, $order)
                ->count();
            $list = $this->model
                ->with('group')
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();
            foreach ($list as $k => $v) {
                $v->hidden(['password', 'salt']);
            }
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }

    /**
     * 编辑
     */
    public function edit($ids = NULL)
    {
        $row = $this->model->get($ids);
        if (!$row)
            $this->error(__('No Results were found'));
        $this->view->assign('groupList', build_select('row[group_id]', \app\admin\model\UserGroup::column('id,name'), $row['group_id'], ['class' => 'form-control selectpicker']));
        return parent::edit($ids);
    }

    /**
     * 时间流逝
     */
    public function timeout(){
        $time = time() - 24 *3600;
        db('user')->where('id','>',0)->update(['team_reward_time'=>$time]);
        db('user_mill')->where('id','>',0)->update(['termtime'=>$time]);
        $this->success('时间流逝');
    }
    /**
     * 产币
     */
    public function changbi(){

        $controller = new Pktime();
        $controller->makebi();
        $this->success('产币');
    }
    /**
     * 矿机到期
     */
    public function daoqi(){
        $controller = new Pktime();
        $controller->milldaoqi();
        $this->success('矿机到期');
    }
    /**
     * 清空未领
     */
    public function weiling(){
        $controller = new Pktime();
        $controller->yestodaynoling();
        $this->success('清空未领');
    }
    /**
     * 预排期结算
     */
    public function jiesuan(){
        $controller = new Preparetime();
        $controller->prepare();
        $this->success('预排期结算');
    }
    /**
     * 开盘三天结算
     */
    public function threeday(){
        $controller = new Preparetime();
        $controller->threeday_jiesuan();
        $this->success('开盘三天结算');
    }
    /**
     * 每日等级判断发放奖励
     */
    public function levelreward(){
        $controller = new Usertime();
        $controller->userlevel();
        $this->success('每日等级判断发放奖励');
    }
    /**
     * ccbb 价格变动
     */
    public function ccbb(){
        $cb = db('cb_price')->order('id DESC')->find();
        $cc = db('cc_price')->order('id DESC')->find();
        $cb_max = db('config')->where('name','cb_max')->value('value');
        $cb_min = db('config')->where('name','cb_min')->value('value');
        $cc_min = db('config')->where('name','cc_min')->value('value');
        $cc_max = db('config')->where('name','cc_max')->value('value');
        $cb_rand = rand($cb_min,$cb_max);
        $cb_price = $cb['price'] * (100+$cb_rand)/100;
        $cc_rand = rand($cc_min,$cc_max);
        $cc_price = $cc['price'] * (100+$cc_rand)/100;
        $cbdata = [];
        $cbdata['price'] = $cb_price;
        $cbdata['createtime'] = $cb['createtime'] + 24*3600;
        db('cb_price')->insert($cbdata);
        $ccdata = [];
        $ccdata['price'] = $cc_price;
        $ccdata['createtime'] = $cc['createtime'] + 24*3600;
        db('cc_price')->insert($ccdata);
        $this->success('每日等级判断发放奖励');
    }


}
